package org.bite.lesson11;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Demo1105 {
    public static void main(String[] args) {
        //创建一个公平锁
        ReentrantLock lock = new ReentrantLock(true);

        //创建一个读写锁
        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
        //获取读锁
        ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
        //获取写锁
        ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();
        //读锁加锁，共享锁，可以多个读锁共存
        readLock.lock();
        //读锁解锁
        readLock.unlock();
        //写锁加锁，排他锁，多个锁不能共存
        writeLock.lock();
        //写锁解锁
        writeLock.unlock();
    }
}
